<?php
/**
 *NewPage Model
 *creates a new page
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 *Class NewPage_Model
 *
 *@package model
 */

class NewPage_Model extends Model{
    /**
     * Holds connection to database
     */
    protected $oDBInstance;
    /**
     * Model constructor
     * Establishes connection to database
     */
    public function __construct(){
        /**
         * Gets the database reference from Mysql_dbconnection
         * @var object
         */
        $this->oDBInstance = Mysql_dbconnection::getInstance();

    }
     
    /*
     * Gets the page title wheter the new page is a content or product page
     * @param page type must be passed
     */
    public function getPageTitle($sPageType){
        if($sPageType == "contentPage"){
            return "Neue Seite anlegen";
        }elseif($sPageType == "category"){
            return "Neue Kategorie anlegen";
        }
    }
    /*
     * Adds the new page into the database
     * @param page type must be passed
     * @param page name
     * @param language of the new page
     * @param parent of the page
     * @param visible
     */
    public function addPage($sPageTyp, $sPageName, $iPageLang, $iPageParent, $iPageVisible){
        $sGermanStandardText = 'Diese Seite ist in deutscher Sprache leider nicht verf&uuml;gbar.';
        $sEnglishStandardText = 'Sorry, this page is not available in english language.';
        $sTurkishStandardText = 'Maalesef, bu sayfa ingilizce dilinde mevcut değildir';

        //Prevents sql injection
        $sPageName = $this->oDBInstance->getSQLValueString($sPageName, 'string');
        if($sPageTyp == 'category'){
            $sGermanPageName = $sPageName;
            $sEnglishPageName = $sPageName;
            $sTurkishPageName = $sPageName;

            $sGermanStandardText = 'F&uuml;r diese Kategorie gibt es noch keine Beschreibung.';
            $sEnglishStandardText = 'Sorry, for this category there is currently no description available.';
            $sTurkishStandardText = 'Üzgünüz, bu kategori için herhangi bir açıklama henüz yok.';
        }else{
            $sConcatenateString = str_replace("'",'',$sPageName);
            $sGermanPageName = "'$sConcatenateString - deutsch'";
            $sEnglishPageName = "'$sConcatenateString - english'";
            $sTurkishPageName = "'$sConcatenateString - turkish'";
        }

        $iPageLang = $this->oDBInstance->getSQLValueString($iPageLang, 'int');
        $iPageParent = $this->oDBInstance->getSQLValueString($iPageParent, 'int');
        $iPageVisible = $this->oDBInstance->getSQLValueString($iPageVisible, 'int');

        if($iPageLang == 3){
            $sTurkishPageName = $sPageName;
        }elseif($iPageLang == 2){
            $sEnglishPageName = $sPageName;
        }else{
            $sGermanPageName = $sPageName;
        }

        //Gets new page level and position for the navigation
        $iPageLevel = $this->getNewPageLevel($iPageParent);
        $iPagePosition = $this->getNewPagePosition($iPageParent);
        //Gets the next page ID
        $iPageID = $this->getPageID();
        //Sets the correct link
        if($sPageTyp == "contentPage"){
            $sPageLink = "index.php?co=$sPageTyp&id=$iPageID";
        }elseif($sPageTyp == "product"){
            $sPageLink = "index.php?co=$sPageTyp&ppid=$iPageID";
        }elseif($sPageTyp == "category"){
            $sPageLink = "index.php?co=product&ca=$iPageID";
        }


        //Performs several inserts in different database tables ( bs_page, bs_page_language)
        $this->oDBInstance->performInsert('id, parent, type, position, link, visible, level', "$iPageID, $iPageParent, '$sPageTyp', $iPagePosition, '$sPageLink', $iPageVisible, $iPageLevel",'bs_page');
        //Saves the page in three different languages with an standard text

        $this->oDBInstance->performInsert('pid, lid, name, content', "$iPageID, 1, $sGermanPageName, '$sGermanStandardText'", 'bs_page_language');
        $this->oDBInstance->performInsert('pid, lid, name, content', "$iPageID, 2, $sEnglishPageName, '$sEnglishStandardText'", 'bs_page_language');
        $this->oDBInstance->performInsert('pid, lid, name, content', "$iPageID, 3, $sTurkishPageName, '$sTurkishStandardText'", 'bs_page_language');

    }


    /*
     * Gets the next page ID
     */
    public function getPageID(){
        $this->oDBInstance->query("SELECT MAX(id) as id FROM `bs_page`");

        $aID = $this->oDBInstance->fetch('array');
        $iID = $aID[0]['id']+1;
        return $iID;
    }
    /*
     * Gets the page level on the basis of the parent
     * @param PageParent
     */
    protected function getNewPageLevel($iPageParent){
        $this->oDBInstance->_setDefaultTable('bs_page');
        $this->oDBInstance->_setDefaultColumn('id');
        if($iPageParent != 'NULL'){
            $this->oDBInstance->performSelect('level', '','','',$iPageParent);
            $aResult = $this->oDBInstance->fetch('array');
            return $aResult[0]['level']+1;
        }else{
            return 1;
        }


    }
    /**
     * Gets the new position of the page (default position in the navigation)
     * @param PageParent
     */
    protected function getNewPagePosition($iPageParent){
        $this->oDBInstance->_setDefaultTable("bs_page");
        $this->oDBInstance->_setDefaultColumn("parent");
        if($iPageParent != 'NULL'){
            $this->oDBInstance->performSelect("MAX(position) as position",'','','',$iPageParent);
        }else {
            $this->oDBInstance->performSelect("MAX(position) as position",'','parent IS NULL');
        }

        $aID = $this->oDBInstance->fetch('array');
        $iID = $aID[0]['position']+1;
        return $iID;
    }



}
?>